function createRedDuckServer(config, id = undefined) {
	let peer = new Peer(id)
	let connections = {}
	let server = {
		peer: peer,
		id: undefined,
		connections: connections,
		getUserList() {
			let userList = []
			for (let clientId in connections) {
				if (connections[clientId])
					userList.push(connections[clientId].username)
			}
			return userList
		},
		send(username, data) {
			for (let clientId in connections) {
				if (connections[clientId].username == username) {
					connections[clientId].dataConnection.send(data)
					break
				}
			}
		},
		start() {
			peer.reconnect()
		},
		close() {
			for (let clientId in connections)
				connections[clientId].dataConnection.close()
			peer.disconnect()
		},
		destroy() {
			peer.destroy()
		}
	}
	peer.on('open', function (id) {
		server.id = peer.id
		console.log('Peer is open. Id:', id)
		toast('Server is open.', 'info')
		config.open(server)
	})
	peer.on('close', function () {
		console.log('Peer is closed')
		toast('Server is closed.', 'info')
	})
	peer.on('error', function (err) {
		console.error('Peer error:', err)
		toast(JSON.stringify(err), 'error', 'Peer Error')
	})
	peer.on('connection', function (dataConnection) {
		let clientId = dataConnection.peer
		console.log('Connection received. PeerId:', clientId)

		let connection = {
			clientId: clientId,
			dataConnection: dataConnection,
			username: undefined,
			activated: false,
		}
		connections[clientId] = connection

		function close() {
			if (connection.username)
				config.deleteUser(server, connection.username)

			connections[clientId] = undefined
			console.log('Connection closed. Id:', clientId)
		}

		dataConnection.on('data', function (data) {
			if (connection.activated) {
				config.data(server, connection.username, data)
			} else {
				if (typeof data === 'string' && data.startsWith(config.handshakeCommand)) {
					let username = data.substring(config.handshakeCommand.length)
					if (username.length) {
						let usernameDuplicate = false
						for (let clientId in connections) {
							if (connections[clientId] && connections[clientId].username == username) {
								usernameDuplicate = true
								break
							}
						}
						if (usernameDuplicate) {
							dataConnection.send({ type: 'error', content: 'Username ' + username + ' has been occupied.' })
							close()
						} else {
							connection.username = username
							connection.activated = true

							config.newUser(server, username)
						}
					} else {
						dataConnection.send({ type: 'error', content: 'Username is empty.' })
						close()
					}
				} else {
					dataConnection.send({ type: 'error', content: 'Handshake message is invalid.' })
					close()
				}
			}
		})
		dataConnection.on('close', close)
		dataConnection.on('error', function (err) {
			console.error('Connection error:', err)
			toast(JSON.stringify(err), 'error', 'Connection Error')
		})
	})
	return server
}